<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      @click="onClickSend"
      class="post-btn"
      :disabled="!message"
    >发布
    </van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/article'
import { Toast } from 'vant'

export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: {
      type: [Array, Object, Number, String],
      required: true
    },
    art_id: {
      type: [Array, Object, Number, String],
      default: null
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {
  },
  mounted () {

  },
  methods: {
    async onClickSend () {
      Toast.loading({
        duration: 0, // 持续展示 toast
        message: '加载中...',
        forbidClick: true
      })
      try {
        const res = await addComment({
          target: this.target + '',
          art_id: this.art_id,
          content: this.message
        })
        this.$emit('onPostSend')
        Toast('发布成功')
        this.$emit('updatecomment', res.new_obj)
        this.message = ''
        console.log(res)
      } catch (e) {
        Toast.fail('异常')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;

  .post-field {
    background-color: #f5f7f9;
  }

  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;

    &::before {
      display: none;
    }
  }
}
</style>
